package com.dl.exam.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.dl.exam.annotation.MethodInfo;
import com.dl.exam.entity.User;
import com.dl.exam.service.CommonService;
import com.dl.exam.util.CaptchaUtils;
import com.dl.exam.util.result.Info;
import com.dl.exam.util.result.Result;
import com.dl.exam.util.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author DL
 * @create 2022-04-21 20:33
 */
@Slf4j
@RestController
@RequestMapping("/common")
public class LoginController {

    @Resource
    private CommonService commonService;

/*    @MethodInfo
    @PostMapping("/login")
    public Result login(HttpSession session, @RequestBody JSONObject jsonObject){

        log.info("{}",jsonObject);
        JSONObject loginForm = jsonObject.getJSONObject("loginForm");
        String username = loginForm.getString("username");
        String password = loginForm.getString("password");
        String captcha = loginForm.getString("captcha");
        String sessionCaptcha = (String) session.getAttribute(Info.SESSION_CAPTCHA.getValue());//获取session中的验证码

        if (captcha.equals("1") || sessionCaptcha.equalsIgnoreCase(captcha) ){//判断验证码
            User user = commonService.login(username, password);//调用服务层登录方法
            System.out.println("user = " + user);
            if (user != null){// 判断账号密码
                if (user.getState() == 0) return Result.error(ResultCodeEnum.ACCOUNT_lockout);//账号锁定
                //StpUtil.login(user.getUserId(), "PC");
                StpUtil.login(user.getUserId(), "PC");
                System.out.println("StpUtil.getTokenInfo() = " + StpUtil.getTokenInfo());
                return Result.success(ResultCodeEnum.SUCCESS_LOGIN)
                        .data(StpUtil.getTokenName(), StpUtil.getTokenValue());
            }else {
                return Result.error(ResultCodeEnum.ACCOUNT_ERROR);//账号或密码错误
            }
        }else {
            return Result.error(ResultCodeEnum.CAPTCHA_ERROR);//验证码错误
        }
    }*/

    @MethodInfo
    @PostMapping("/login")
    public Result login(@RequestBody JSONObject jsonObject){
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");

        User user = commonService.login(username, password);//调用服务层登录方法
        System.out.println("user = " + user);
        if (user != null){// 判断账号密码
            if (user.getState() == 0) return Result.error(ResultCodeEnum.ACCOUNT_lockout);//账号锁定
            //StpUtil.login(user.getUserId(), "PC");
            StpUtil.login(user.getUserId(), "PC");
            //System.out.println("StpUtil.getTokenInfo() = " + StpUtil.getTokenInfo());
            return Result.success(ResultCodeEnum.SUCCESS_LOGIN)
                    .data(StpUtil.getTokenName(), StpUtil.getTokenValue())
                    .data("name", user.getName());
        }else {
            return Result.error(ResultCodeEnum.ACCOUNT_ERROR);//账号或密码错误
        }
    }

    @GetMapping("/logout")
    public Result logout(){
        // 当前会话注销登录
        StpUtil.logout();
        return Result.ok();
    }


    @GetMapping("/captcha")
    public void captcha(HttpSession session, HttpServletResponse response) {
        //设置验证码位数
        String captcha = CaptchaUtils.generateVerifyCode(4);
        //验证码放入session
        session.setAttribute(Info.SESSION_CAPTCHA.getValue(), captcha);
        try {
            //获得输出流
            ServletOutputStream os = response.getOutputStream();
            //设置响应头
            response.setContentType("image/png");
            //response.setContentType("application/octet-stream");
            //获得验证码图片ConstantUtils.SESSION_Captcha.getValue()
            CaptchaUtils.outputImage(150,60, os, captcha);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
